package com.xingdata.jjxc.activity;

import jar.tools.UdpTools;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.Common;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.adapter.DrivingRecordAdapter;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.enty.TravelRecordEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.http.HttpUtil;
import com.xingdata.jjxc.utils.SharedPreTools;
import com.xingdata.jjxc.utils.StringUtil;
import com.xingdata.jjxc.utils.SystemInfo;
import com.xingdata.jjxc.views.MyListView;
import com.xingdata.jjxc.views.MyListView.OnLoadMoreListener;
import com.xingdata.jjxc.views.MyListView.OnRefreshListener;

public class TravelRecordListView extends Base1Activity implements
		OnLoadMoreListener, OnRefreshListener, OnItemClickListener {
	private String TAG = "TravelRecordListView";
	private MyListView listview;
	private DrivingRecordAdapter adapter;
	private ArrayList<TravelRecordEntity> entities = new ArrayList<TravelRecordEntity>();
	private int startNumber = 0;
	private int pageNumber = 500;

	@Override
	protected int getContentViewId() {
		return R.layout.travelrecordlistview;
	}

	@Override
	protected String getTitleText() {
		// TODO Auto-generated method stub
		return "里程记录";
	}

	@Override
	protected void initViews() {
		adapter = new DrivingRecordAdapter(this, entities);
		listview = (MyListView) findViewById(R.id.drivinghabits_listview);
		listview.setCanLoadMore(true);
		// 向前刷新
		listview.setOnRefreshListener(this);
		// 加载更多
		listview.setOnLoadListener(this);
		listview.setCanRefresh(false);
		listview.setOnItemClickListener(this);
		listview.setAdapter(adapter);
		doPost_findobdTrip();
	}

	private void doPost_findobdTrip() {
		paramsMap = new HashMap<String, String>();
		paramsMap.put("userid", SystemInfo.getUserInfo().getUserid());
		paramsMap.put("access_token", SystemInfo.getUserInfo().getToken());
		if (StringUtil.isStringNull(UdpTools.getHudCode())) {
			paramsMap.put("hud_sn", UdpTools.getHudCode());
		} else {
			paramsMap.put("hud_sn",
					SharedPreTools.getString(this, Common.HUD_SN, ""));
		}

		paramsMap.put("beginnum", startNumber + "");
		paramsMap.put("endnum", (startNumber + pageNumber) + "");

		HttpUtil.getInstance(this).Post(App.ZZD_REQUEST_FINDOBDTRIP, paramsMap,
				new HttpResponseListener() {

					@Override
					public void onSuccess(RespEntity respEntity) {
						if (respEntity.getResult().length() == 0) {
							showToast(respEntity.getMsg());
							return;
						}
						List<TravelRecordEntity> arrayList = JSON.parseArray(
								respEntity.getResult(),
								TravelRecordEntity.class);

						if (arrayList.size() == 0) {
							startNumber = 0;
							listview.onLoadMoreComplete();
							showToast("加载完毕");
						} else {
							if (arrayList.size() != pageNumber) {
								startNumber = startNumber + arrayList.size();
							} else {
								startNumber = startNumber + pageNumber;
							}
							entities.addAll(arrayList);
							adapter.notifyDataSetChanged();
							listview.onLoadMoreComplete();
						}

					}

					@Override
					public void onLoading(int vurrent) {
						// TODO Auto-generated method stub
						listview.onLoadMoreComplete();
					}

					@Override
					public void onFailure(String result) {
						showToast(result);
						listview.onLoadMoreComplete();
					}
				});

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long id) {
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putSerializable("entity",
				(Serializable) entities.get(position - 1).getDetail());
		intent.putExtras(bundle);
		intent.setClass(this, DrivingHabitsListView.class);
		startActivity(intent);
	}

	@Override
	public void onRefresh() {

	}

	@Override
	public void onLoadMore() {
		doPost_findobdTrip();
	}

}
